package com.shenzhen.horselive.ui.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.flqy.baselibrary.utils.L;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.adapter.WealthAdapter;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.BaseFragment;
import com.shenzhen.horselive.beans.CommResponseDataBean;
import com.shenzhen.horselive.beans.SumBean;
import com.shenzhen.horselive.beans.UserAcountBean;
import com.shenzhen.horselive.beans.WealthBean;
import com.shenzhen.horselive.databinding.FragmentWealthBinding;
import com.shenzhen.horselive.databinding.HeadWealthBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.ViewObserver;
import com.shenzhen.horselive.ui.activity.EmptyActivity;
import com.shenzhen.horselive.ui.activity.FenZhiDuiActivity;
import com.shenzhen.horselive.ui.activity.FenZhiJiaoYiActivity;
import com.shenzhen.horselive.ui.activity.FenZhiValueActivity;
import com.shenzhen.horselive.ui.activity.HongBaoGuValueActivity;
import com.shenzhen.horselive.ui.activity.MyWalletActivity;
import com.shenzhen.horselive.utils.AnimationUtils;

import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link WealthFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class WealthFragment extends BaseFragment implements View.OnClickListener {


    public WealthFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment WealthFragment.
     */
    public static WealthFragment newInstance() {
        WealthFragment fragment = new WealthFragment();
//        Bundle args = new Bundle();
//        fragment.setArguments(args);
        return fragment;
    }
    FragmentWealthBinding  binding;
    @Override
    protected View getContentView(LayoutInflater inflater, ViewGroup viewGroup) {
       binding = FragmentWealthBinding.inflate(inflater,viewGroup,false);
       initViews();
       return binding.getRoot();
    }

    @Override
    protected void loadDataOnece() {
        super.loadDataOnece();
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initViews() {
        //峰值兑换
        binding.imgFenzhiRecharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), FenZhiDuiActivity.class));
            }
        });
        //峰值交易
        binding.imgFenzhiTrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), FenZhiJiaoYiActivity.class));
            }
        });
        //峰值价值
        binding.containerFenzhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //峰值的价值
                startActivity(new Intent(getContext(), FenZhiValueActivity.class));
            }
        });

        //钱包
        binding.myWallet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), MyWalletActivity.class));
            }
        });
        //红包股
        binding.myGu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), HongBaoGuValueActivity.class));
            }
        });

        //刷新
        binding.smartRefresh.setEnableLoadMore(false);
        binding.smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                initData();
            }
        });

        binding.myShitou.setOnClickListener(this);
        binding.myChaojiKuang.setOnClickListener(this);
        binding.myFenjingKu.setOnClickListener(this);
        binding.myPos.setOnClickListener(this);
        binding.myShouqian.setOnClickListener(this);


    }

    private void initData() {
        //总资产
        ApiFactory.getApiService().account(User.get().getMyInfo().getToken())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new ViewObserver<CommResponseDataBean>(this) {
                    @Override
                    protected void onNextInActive(CommResponseDataBean commResponseDataBean) {
                        if (commResponseDataBean.isSuccess())
                        {
//                            NumberFormat  format = NumberFormat.getInstance();
//                            format.setMaximumFractionDigits(2);
//                            binding.tvTotalMoney.setText("¥"+format.format(Double.parseDouble(commResponseDataBean.getData())));
                            AnimationUtils.addTextViewAddAnim(binding.tvTotalMoney,Double.parseDouble(commResponseDataBean.getData()),"¥");

                        }
                    }
                });


        //获取峰值
        getAcountForType(1);
       //获取钱包
        getAcountForType(0);
        //获取红包股
        getAcountForType(2);

        //获取今天的收益
        getAcountSumForType(0);
        getAcountSumForType(1);
        getAcountSumForType(2);

    }

    /**
     * 获取收益
     * @param i
     */
    //帐户类型: 0:人民币帐户 ,1:蜂值帐户 ,2:红包股帐户 ,3:金卡帐户
    private void getAcountSumForType(int i) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        String  staredtime =  sf.format(c.getTime());
        c.add(Calendar.DAY_OF_MONTH, 1);
        String  endtime =  sf.format(c.getTime());
        ApiFactory.getApiService().accountTypesSum(User.get().getMyInfo().getToken()
        ,i+"","1",staredtime,endtime)
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new ViewObserver<SumBean>(this) {
                    @Override
                    protected void onNextInActive(SumBean sumBean) {
                        if (binding.smartRefresh.isRefreshing())
                            binding.smartRefresh.finishRefresh();
                        if (sumBean.isSuccess())
                        {
                            switch (i)
                            {
                                case 0:
                                    //钱包收益
                                    binding.tvItemValue1.setText("¥"+formatFuckingNumber(sumBean.getData().getSumValue()));
                                    break;
                                case 1:
                                    //峰值收益
                                    binding.tvFenzhiProfit.setText(""+formatFuckingNumber(sumBean.getData().getSumAmount())+"个");
                                    break;
                                case 2:
                                    //红包股收益
                                    binding.tvItem1Value1.setText(formatFuckingNumber(sumBean.getData().getSumAmount())+"股");
                                    break;
                            }
                        }
                    }
                });

    }

    //帐户类型: 0:人民币帐户 ,1:蜂值帐户 ,2:红包股帐户 ,3:金卡帐户
    private void getAcountForType(int i) {
        ApiFactory.getApiService().accountTypes(User.get().getMyInfo().getToken(),i+"")
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new ViewObserver<UserAcountBean>(this) {
                    @Override
                    protected void onNextInActive(UserAcountBean userAcountBean) {
                        if (userAcountBean.isSuccess())
                        {
                            NumberFormat  numberFormat = NumberFormat.getNumberInstance();
                            numberFormat.setMaximumFractionDigits(10);//最多保留10位
                          double  value = Double.parseDouble(userAcountBean.getData().getValue());
//                          value =  userAcountBean.getData().getValue();
                          switch (i)
                        {
                            case 0:
                               //钱包
                                AnimationUtils.addTextViewAddAnim(binding.tvItemValue,value,"¥","");
//                              binding.tvItemValue.setText(value+"/元");
                              break;
                            case 1:
                                //峰值账户
                                AnimationUtils.addTextViewAddAnim(binding.tvFenzhiValue,value,"¥","");
//                                binding.tvFenzhiValue.setText(value+"/元");
                                //峰值数量
                                if (userAcountBean.getData().getAmount()==null||"0E-10".equals(userAcountBean.getData().getAmount()))
                                {
                                    binding.tvFenzhiNumbers.setText("0");

                                }
                                else
                                binding.tvFenzhiNumbers.setText(userAcountBean.getData().getAmount()+"");
                                break;
                            case 2:
                                //红包股
                                AnimationUtils.addTextViewAddAnim(binding.tvItem1Value,value,"¥","");
//                                binding.tvItem1Value.setText(value+"/元");
                                break;

                        }
                        }
                    }
                });

    }


    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarColor(R.color.red_common)
                .statusBarDarkFont(false)
                .init();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.my_shitou:
                EmptyActivity.start(getContext(),"我的矿石");
                break;
            case R.id.my_chaoji_kuang:
                EmptyActivity.start(getContext(),"超级矿");
                break;
            case R.id.my_fenjing_ku:
                EmptyActivity.start(getContext(),"分金库");
                break;
            case R.id.my_pos:
                EmptyActivity.start(getContext(),"POS公排");
                break;
            case R.id.my_shouqian:
                EmptyActivity.start(getContext(),"收钱吧");
                break;
        }
    }
}